import {Component, OnDestroy, OnInit} from '@angular/core';
import {Observable} from 'rxjs/Observable';
import {Message} from '@stomp/stompjs';

import {Subscription} from 'rxjs/Subscription';
import {StompService} from '@stomp/ng2-stompjs';
import {stompConfig} from '../stompConfig';
import {DataStorageUtil} from '../../../util/data-storage-util';
import {AppConstants} from '../../../constants/constants';

@Component({
  selector: 'app-stomp-rawdata',
  templateUrl: './stomp-rawdata.component.html',
  styleUrls: ['./stomp-rawdata.component.css']
})
export class StompRawdataComponent implements OnInit, OnDestroy {

  userCode: string;

  // Stream of messages
  private subscription: Subscription;
  public messages: Observable<Message>;

  // Subscription status
  public subscribed: boolean;

  // Array of historic message (bodies)
  public mq: Array<string> = [];

  public noAckMessages: Array<Message> = [];

  // A count of messages received
  public count = 0;

  private _counter = 1;

  /** Constructor */
  constructor(private stompService: StompService,
              private dataStorage:DataStorageUtil) {
  }

  ngOnInit() {
    this.subscribe();
  }

  public subscribe() {

    if (this.subscribed) {
      return;
    }
    // Stream of messages
    // this.messages = this.stompService.subscribe(this.dataStorage.get(AppConstants.accountName), stompConfig.headers);
    this.messages = this.stompService.subscribe("/queue/pulin00", stompConfig.headers);

    // Subscribe a function to be run on_next message
    this.subscription = this.messages.subscribe(this.on_next);
  }

  public unsubscribe() {

    if (!this.subscribed) {
      return;
    }

    // This will internally unsubscribe from Stomp Broker
    // There are two subscriptions - one created explicitly, the other created in the template by use of 'async'
    this.subscription.unsubscribe();
    this.subscription = null;
    this.messages = null;
    this.noAckMessages = null;
    this.mq = null;

    this.subscribed = false;
  }

  ngOnDestroy() {
    this.unsubscribe();
  }

  public onSendMessage(message: string) {

    this.stompService.publish('alarmReplyTo',
      `${message}`);
  }

  /** Consume a message from the _stompService */
  public on_next = (message: Message) => {
    // message.ack(stompConfig.headers);
    // let mes = message.body.split('##');
    let t = JSON.parse(message.body);
    //this.stompService.client.nack();
    // Store message in "historic messages" queue
    this.mq.push(t.content + '\n');

    // Count it
    this.count++;
    // if (this.count > 40) {
    //   message.ack();
    // }
    this.noAckMessages.push(message);
    // Log it to the console
    let messagId = message.headers['message-id'];
    // if (this.count > 10) {
    //   this.setAckMessage(message);
    // }
    this.onSendMessage(t.id);

  }

  public setAckMessage(message: Message) {
    if (!!message) {
      message.ack();
    }
  }

  remve() {
    this.noAckMessages.forEach(function (value, index, array) {
      if (index > 4) {
        value.ack(stompConfig.headers);
      }
    }, this);
  }
}
